#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p "$path"; else path=$path$(date "+%y%m%d%H%M%S") && mkdir -p "$path"; fi
    echo "$path"
}

buildpath=$(create_dir "$1")
installpath=$(create_dir "$2")

yum install -y m4 environment-modules systemd-devel
source /etc/profile
module purge

set -e
yum -y install gcc gcc-c++ openssl openssl-devel
yum install -y gperf bison flex nss-devel dbus-devel nodejs wayland-devel \
libxkbcommon-devel llvm-devel libstdc++-static mesa-libGL-devel at-spi2-atk-devel \
fontconfig-devel libXcursor-devel libXcomposite-devel mesa-libEGL-devel libproxy-devel \
libproxy libXdamage-devel expat-devel ninja-build clang-devel
yum install -y libxml2 libxml2-devel libxslt libxslt-devel python3-devel patch qt-devel subversion subversion-devel

dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh "$buildpath" "$installpath"
module use "$installpath"/bisheng-compiler-2.1.0
module load "$installpath"/bisheng-compiler-2.1.0/bisheng_modulefiles

bash $dep_dir/cmake-3.17.3.sh "$buildpath" "$installpath"
module use "$installpath"/cmake-3.17.3
module load "$installpath"/cmake-3.17.3/cmake_modulefiles
bash $dep_dir/qt5-5.13.2.sh "$buildpath" "$installpath"
module use "$installpath"/qt5-5.13.2
module load "$installpath"/qt5-5.13.2/qt5_modulefiles

bash 3dslicer-5.0.3.sh "$buildpath" "$installpath"
module use "$installpath"/3dslicer-5.0.3
module load "$installpath"/3dslicer-5.0.3/3dslicer_modulefiles

mkdir -p pkg/3DSlicer-5.0.3-hpc && cp -r "$installpath"/* pkg/3DSlicer-5.0.3-hpc/
cat>"pkg/3DSlicer-5.0.3-hpc/run.sh"<<EOF
#!/bin/bash
sudo yum install -y m4 environment-modules systemd-devel csh time patch libatomic perl-Data-Dumper \\
autoconf automake libtool numactl binutils valgrind flex wget tar libatomic gcc-c++ git 
sudo yum -y install gcc gcc-c++ openssl openssl-devel
sudo yum install -y gperf bison flex nss-devel dbus-devel nodejs wayland-devel \\
libxkbcommon-devel llvm-devel libstdc++-static mesa-libGL-devel at-spi2-atk-devel \\
fontconfig-devel libXcursor-devel libXcomposite-devel mesa-libEGL-devel libproxy-devel \\
libproxy libXdamage-devel expat-devel ninja-build clang-devel
sudo yum install -y libxml2 libxml2-devel libxslt libxslt-devel python3-devel patch qt-devel subversion subversion-devel

source /etc/profile &> /dev/null 
current_dir="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd -P)"
for f in "\${current_dir}"/*; do
  if [[ -d "\${f}" && -f "\$(ls "\${f}"/*modulefiles 2>&1)" ]]; then
    module use "\${f}" && module load "\${f}"/*modulefiles
  fi
done

cd \${current_dir}/3dslicer-5.0.3/build
sed -i "s/https:\\/\\/ghproxy.com\\/https:\\/\\/github.com/https:\\/\\/github.com/g" \`grep -rl "https://ghproxy.com/https://github.com/" ../*\`
sed -i "s/\\\${EP_GIT_PROTOCOL}:\\/\\/github.com/https:\\/\\/github.com/g" \`grep -rl "\\\${EP_GIT_PROTOCOL}://github.com" ../*\`
cmake -DADDITIONAL_CXX_FLAGS=-fPIC -DADDITIONAL_C_FLAGS=-fPIC -DCMAKE_C_COMPILER=\${current_dir}/bisheng-compiler-2.1.0/bin/clang -DCMAKE_CXX_COMPILER=\${current_dir}/bisheng-compiler-2.1.0/bin/clang++ -DSlicer_FORCED_WC_LAST_CHANGED_DATE=2022-07-08 -Wno-dev ../
sed -i "s/https:\\/\\/github.com/https:\\/\\/ghproxy.com\\/&/g" \`grep -rl "https://github.com/" ../*\`

cnt=1
number=10
while [ \$cnt -lt \$number ] 
do  
    echo "第\$cnt次尝试构建3DSlicer,构建时间较长,请耐心等待......"
    make -j\$(nproc)
    result_code=\$?
    if [[ \$result_code -ne 0 ]]; then
        cnt=\$[ \$cnt + 1 ]
    else
        echo "3DSlicer构建成功!"
        break
    fi
done

if [ \$cnt -eq \$number ]; then
    echo "3DSlicer构建失败,请重新执行source run.sh"
    return 1
fi

cd \${current_dir}/
echo -e "\033[1;32;1m3DSlicer environment initialization completed.\033[0m"

EOF
cd pkg/ && tar zcvf 3DSlicer-5.0.3-hpc.tar.gz 3DSlicer-5.0.3-hpc

rm -rf "$buildpath" "$installpath"
